package cn.lingyangwl.agile.gateway.handler;

import cn.lingyangwl.agile.model.enums.StorageTypeEnum;
import cn.lingyangwl.agile.model.module.config.param.StorageParam;
import cn.lingyangwl.framework.tool.core.UrlUtils;
import org.springframework.stereotype.Component;

/**
 * @author shenguangyang
 */
@Component
public class MinioFileHandlerImpl extends AbstractFileHandler {

    @Override
    public StorageTypeEnum storageType() {
        return StorageTypeEnum.MINIO;
    }

    @Override
    public String getAccessUrl(String bucketName, StorageParam.Domain domain) {
        return domain.getDomain() + "/" + bucketName;
    }

    @Override
    public String realToVirtualBaseUrl(String data, StorageParam<?> storageParam, String domain) {
        String virtualBaseUrl = getVirtualBaseUrl(storageParam);
        return data.replace(UrlUtils.removeRepeatSlashOfUrl(domain + "/" + storageParam.getBucket()), virtualBaseUrl);
    }

    private String getVirtualBaseUrl(StorageParam<?> storageParam) {
        String format = String.format("http://%s.%s.file", "1", storageParam.getBucket());
        return UrlUtils.removeRepeatSlashOfUrl(format);
    }
}
